/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.sync.service;

import com.pitop.core.entity.BeanRet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import com.pitop.sync.api.SyncNewDataFeignApi;
import com.pitop.core.exceptions.SyncNewDataException;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.entity.Page;
import com.pitop.sync.entity.SyncNewData;
import org.springframework.web.bind.annotation.RequestBody;


/**
 * 需要同步的数据
 *
 * @author pi-top
 */
@Service("syncNewDataSVImpl")
@Slf4j
public class SyncNewDataSVImpl {

    @Autowired
    private SyncNewDataFeignApi syncNewDataFeignApi;

    /**
     * 加载一个对象SyncNewData
     *
     * @param id id* @param code 编码* @param factoryCode 工厂编码* @param dataCode 数据编码
     * @return SyncNewData
     */
    public SyncNewData load(Long id, String code, String factoryCode, String dataCode) {
        if (id == null && code == null && factoryCode == null && dataCode == null) {
            throw new SyncNewDataException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return syncNewDataFeignApi.load(id, code, factoryCode, dataCode);
    }

    /**
     * 加载一个对象SyncNewData 通过id
     *
     * @param id id
     * @return SyncNewData
     */
    public SyncNewData loadById(Long id) {
        if (id == null) {
            throw new SyncNewDataException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return syncNewDataFeignApi.loadById(id);
    }

    /**
     * 加载一个对象SyncNewData 通过code
     *
     * @param code 编码
     * @return SyncNewData
     */
    public SyncNewData loadByCode(String code) {
        if (code == null) {
            throw new SyncNewDataException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return syncNewDataFeignApi.loadByCode(code);
    }

    /**
     * 加载一个对象SyncNewData 通过factoryCode
     *
     * @param factoryCode 工厂编码
     * @return SyncNewData
     */
    public SyncNewData loadByFactoryCode(String factoryCode) {
        if (factoryCode == null) {
            throw new SyncNewDataException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return syncNewDataFeignApi.loadByFactoryCode(factoryCode);
    }

    /**
     * 加载一个对象SyncNewData 通过dataCode
     *
     * @param dataCode 数据编码
     * @return SyncNewData
     */
    public SyncNewData loadByDataCode(String dataCode) {
        if (dataCode == null) {
            throw new SyncNewDataException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return syncNewDataFeignApi.loadByDataCode(dataCode);
    }


    /**
     * 删除对象SyncNewData
     *
     * @param id id* @param code 编码* @param factoryCode 工厂编码* @param dataCode 数据编码
     * @return SyncNewData
     */

    public void delete(Long id, String code, String factoryCode, String dataCode) {
        if (id == null && code == null && factoryCode == null && dataCode == null) {
            throw new SyncNewDataException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        syncNewDataFeignApi.delete(id, code, factoryCode, dataCode);
    }


    /**
     * 查询SyncNewData分页
     *
     * @param syncNewData 对象
     * @param curPage     当前页
     * @param pageSize    分页条数
     * @return List<SyncNewData>
     */
    public List<SyncNewData> list(SyncNewData syncNewData, int curPage, int pageSize) {
        return syncNewDataFeignApi.list(syncNewData, curPage, pageSize);
    }

    /**
     * 查询SyncNewData分页
     *
     * @param id       id* @param code  编码* @param factoryCode  工厂编码* @param dataCode  数据编码
     * @param curPage  当前页
     * @param pageSize 分页条数
     * @return List<SyncNewData>
     */
    public List<SyncNewData> list(Long id, String code, String factoryCode, String dataCode, int curPage, int pageSize) {
        if (curPage < 0) {
            curPage = 1;
        }

        if (pageSize < 0) {
            pageSize = Page.limit;
        }

        SyncNewData syncNewData = new SyncNewData();
        syncNewData.setId(id);
        syncNewData.setCode(code);
        syncNewData.setFactoryCode(factoryCode);
        syncNewData.setDataCode(dataCode);

        return syncNewDataFeignApi.list(syncNewData, curPage, pageSize);
    }


    public int count(Long id, String code, String factoryCode, String dataCode) {
        return syncNewDataFeignApi.count(id, code, factoryCode, dataCode);
    }


    public int count(SyncNewData syncNewData) {
        return syncNewDataFeignApi.count(syncNewData);
    }


    /**
     * 保存
     *
     * @param syncNewData 实体
     * @throws BaseException
     */
    public void save(SyncNewData syncNewData) {
        if (syncNewData == null) {
            throw new SyncNewDataException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        syncNewDataFeignApi.build(syncNewData);
    }

    /**
     * 更新
     *
     * @param syncNewData 实体
     * @throws BaseException
     */
    public void modify(SyncNewData syncNewData) {
        if (syncNewData == null) {
            throw new SyncNewDataException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        syncNewDataFeignApi.modify(syncNewData);
    }

    /**
     * 从云端往工厂端同步数据调度接口
     *
     * @param factoryCode
     * @return
     */
    public BeanRet syncDataToDown(String factoryCode) {
        return syncNewDataFeignApi.syncDataToDown(factoryCode);

    }

    /**
     * 更新同步数据状态
     *
     * @param syncResult
     * @return
     */
    public BeanRet updateStateBySyncResult(String syncResult) {
        return syncNewDataFeignApi.updateStateBySyncResult(syncResult);
    }
}
